package com.rdc.qingkou.presenter;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.rdc.qingkou.base.BasePresenterImpl;
import com.rdc.qingkou.contract.SendContract;
import com.rdc.qingkou.entity.Card;
import com.rdc.qingkou.entity.SendContentBean;
import com.rdc.qingkou.model.SendModel;
import com.rdc.qingkou.model.imodel.ISendCallBack;
import com.rdc.qingkou.utils.CompressImageUtil;
import com.zxy.tiny.callback.FileCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;


public class SendPresenter extends BasePresenterImpl<SendContract.View> implements SendContract.PresenterI {
    private SendModel sendModel = new SendModel();
    private SendContentBean mSendContentBean;
    private File mMediaFile;
    private File mVideoFile;
    private List<String> mLabel;
    private Card mCard;
    private static final String TAG = "SendPresenter";


    @Override
    public void takePhoto() {

        mView.takePhoto(sendModel.getImageUri());

    }

    @Override
    public void chooseAlbum() {
        mView.openAlbum();
    }

    @Override
    public void setAlbumPhoto(Intent intent) {
        if (Build.VERSION.SDK_INT >= 19) {
            mView.setAlbumImage(sendModel.handleImageOnKitKat(intent));
        } else {
            Uri uri = intent.getData();
            mView.setAlbumImage(sendModel.getImagePath(uri, null));

        }

    }


    @Override
    public void send() {
        mView.showLoadingDialog();

        mSendContentBean = mView.getSendBean();
        mLabel = new ArrayList<>();
        for (String tag : mSendContentBean.getmLabel()) {
            if (!TextUtils.isEmpty(tag)) {
                mLabel.add(tag);
            }

        }
        mCard = new Card();
        mCard.setMediaType(mSendContentBean.isMediaType());
        mCard.setContent(mSendContentBean.getmContent());
        mCard.setAnonymity(mSendContentBean.getAnonymity());
        if (!TextUtils.isEmpty(mSendContentBean.getmVoiceFile())) {
            mVideoFile = new File(mSendContentBean.getmVoiceFile());
        }


        if (!TextUtils.isEmpty(mSendContentBean.getmMediaFile())) {
            if (mSendContentBean.isMediaType()) {
                CompressImageUtil.compressImage(mSendContentBean.getmMediaFile(), new FileCallback() {
                    @Override
                    public void callback(boolean isSuccess, String outfile, Throwable t) {
                        if (isSuccess) {
                            mMediaFile = new File(outfile);
                            sendCard();

                        }else {
                            mMediaFile = new File(mSendContentBean.getmMediaFile());
                            sendCard();

                        }

                    }
                });

            } else {

                mMediaFile = new File(mSendContentBean.getmMediaFile());
                sendCard();



            }
        }






    }

    public void sendCard(){
        sendModel.send(mMediaFile, mVideoFile, mCard, mLabel, new ISendCallBack() {
            @Override
            public void setDisposable(Disposable disposable) {
                mView.setDisposable(disposable);
            }

            @Override
            public void success() {

                mView.dimissLoadingDialog();

                mView.jumpToHomePage();
                mView.finishActivity();

            }

            @Override
            public void fail() {
                mView.dimissLoadingDialog();
                mView.showFailure();

            }
        });
    }
}








